﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ccit.Utils;
using System.Data;
using Ccit.ClassLibrary;

namespace Ccit.SysPlat.Organ
{
    /// <summary>
    /// Description: 系统平台组织机构 -> 用户管理业务层
    /// Author: tim 2010-02-10
    /// </summary>
    public class UserBLL
    {
        #region initialize 

        IUser _userProcessor = null;

        public UserBLL()
        {
            _userProcessor = Factory.CreateObject(MsUser.assemblyName, MsUser.className, ImportComponentType.DotNetDll, null) as IUser;
        }

        public void Release()
        {
            _userProcessor = null;
        }
        #endregion

        #region public methods
        /// <summary>
        /// 获取用户集合
        /// </summary>
        /// <param name="whereClause">过滤条件</param>
        /// <returns></returns>
        public DataSet GetUserList(MsUser whereClause)
        {
            DataSet dSet = _userProcessor.GetUserList(whereClause);
            return dSet;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userIds">用户Id串</param>
        /// <returns>
        ///     【0】: 操作结果
        ///     【1】: 提示消息
        /// </returns>
        public List<string> Delete(string userIds)
        {
            if (string.IsNullOrEmpty(userIds)) return new List<string> { "-999", "操作失败:缺少必要参数!" };

            List<string> lstResult = _userProcessor.Delete(userIds);
            return lstResult;
        }

        /// <summary>
        /// 恢复用户记录
        /// </summary>
        /// <param name="userId">用户Id串</param>
        /// <returns>
        ///     【0】: 操作结果
        ///     【1】: 提示消息
        /// </returns>
        public List<string> Resume(string userIds)
        {
            if (string.IsNullOrEmpty(userIds)) return new List<string> { "-999", "操作失败:缺少必要参数!" };

            List<string> lstResult = _userProcessor.Resume(userIds);
            return lstResult;
        }

        /// <summary>
        /// 恢复用户默认口令
        /// </summary>
        /// <param name="userIds">用户Id串</param>
        /// <returns>
        ///     【0】: 操作结果
        ///     【1】: 提示消息
        /// </returns>
        public List<string> ResumeDefPwd(string userIds)
        {
            if (string.IsNullOrEmpty(userIds)) return new List<string> { "-999", "操作失败:缺少必要参数!" };

            List<string> lstResult = _userProcessor.ResumeDefPwd(userIds);
            return lstResult;
        }

        /// <summary>
        /// 向上调整用户显示顺序
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="curOrder">调整前显示顺序</param>
        /// <returns>
        ///     【0】: 操作结果
        ///     【1】: 提示消息
        /// </returns>
        public List<string> MoveUp(string userId, string curOrder)
        {
            if (string.IsNullOrEmpty(userId)) return new List<string> { "-999", "操作失败:缺少必要参数!" };

            List<string> lstResult = _userProcessor.MoveUp(userId, curOrder);
            return lstResult;
        }

        /// <summary>
        /// 向下调整用户显示顺序
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="curOrder">调整前显示顺序</param>
        /// <returns>
        ///     【0】: 操作结果
        ///     【1】: 提示消息
        /// </returns>
        public List<string> MoveDown(string userId, string curOrder)
        {
            if (string.IsNullOrEmpty(userId)) return new List<string> { "-999", "操作失败:缺少必要参数!" };

            List<string> lstResult = _userProcessor.MoveDown(userId, curOrder);
            return lstResult;
        }

        /// <summary>
        /// 获取用户消息对象
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="enContainExtend">是否获取扩展属性</param>
        /// <returns></returns>
        public MsUser GetUserInfo(string userId,eTrueFalse enContainExtend)
        {
            if (string.IsNullOrEmpty(userId)) return null;

            return _userProcessor.GetUserInfo(userId,enContainExtend);
        }

        /// <summary>
        /// 登录名同名校验
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public List<string> IsExistLoginName(string loginName, string userId)
        {
            bool bExist = DataInterface.CheckNameIsExist("SYS_A1", "SYS_A1_10", "SYS_A1_20", loginName, userId);           
            return ((bExist) ? new List<string> { "EXIST", "登录名已存在!" } : new List<string> { "NOEXIST", string.Empty });
        }

        /// <summary>
        /// 保存用户记录
        /// </summary>
        /// <param name="msUser">用户信息消息对象</param>
        /// <returns>
        ///     【0】: 操作结果
        ///     【1】: 提示消息
        /// </returns>
        public List<string> Save(MsUser msUser)
        {
            if (null == msUser) return new List<string> { "-999", "操作失败:缺少必要参数!" };

            return _userProcessor.Save(msUser);
        }

        ///// <summary>
        ///// 校验用户口令
        ///// </summary>
        ///// <param name="userId">用户Id</param>
        ///// <param name="enUserType">用户类型</param>
        ///// <param name="oriPwd">用户原始口令</param>
        ///// <returns></returns>
        //public List<string> CheckUserPwd(string userId, eUserType enUserType, string oriPwd)
        //{
        //    if (string.IsNullOrEmpty(userId)) return new List<string> { "-999", "操作失败:缺少必要参数!" };

        //    return _userProcessor.CheckUserPwd(userId, enUserType, oriPwd);
        //}

        /// <summary>
        /// 修改用户口令
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="enUserType">用户类型</param>
        /// <param name="newPwd">新口令</param>
        /// <returns></returns>
        public List<string> ModifyPwd(string userId, eUserType enUserType, string oriPwd, string newPwd)
        {
            if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(newPwd)) return new List<string> { "-999", "操作失败:缺少必要参数!" };

            List<string> lstResult = _userProcessor.CheckUserPwd(userId, enUserType, oriPwd);
            if (Int32.Parse(lstResult[0]) < 0) return lstResult;

            return _userProcessor.ModifyPwd(userId, enUserType, newPwd);
        }
        #endregion
    }

   
}
